<?php
App::uses('AppController', 'Controller');

class AlumnosController extends AppController {
var $name = 'Alumnos';
	public $components = array('Search.Prg');

	public $presetVars = array( 
		
		array('field' => 'nombre', 'type' => 'value'),
	);
	
	public $paginate = array(
		'limit' => 3,
		'order' => array(
				'Alumno.id' => 'asc'
				)
		);

      
	public function index() {
	
        $this->Prg->commonProcess();
		$this->paginate = array( 
			'conditions' => $this->Alumno->parseCriteria($this->passedArgs));
		$this->set('alumnos', $this->paginate());
		$apoderados=$this->Alumno->Apoderado->find('list',array('fields'=>'Apoderado.nombres'));
                
	$this->set(compact('apoderados','alumnos'));
	
	}   
		
		public function view($id = null) {
		$this->Alumno->id = $id;
		if (!$this->Alumno->exists()) {
			throw new NotFoundException(__('Rol no válido'));
		}
		$this->set('alumno', $this->Alumno->read(null, $id));
	}


	public function add() {
		if ($this->request->is('post')) {
			$this->Alumno->create();
			if ($this->Alumno->save($this->request->data)) {
				$this->Session->setFlash(__('El alumno ha sido guardado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El Alumno no puede ser guardado. Intente otra vez.'));
			}
		}
                
                  $apoderados=$this->Alumno->Apoderado->find('list',array('fields'=>'Apoderado.nombres'));
                
	$this->set(compact('apoderados','alumnos'));
	}
	
	
		public function edit($id = null) {
		$this->Alumno->id = $id;
		if (!$this->Alumno->exists()) {
			throw new NotFoundException(__('Alumno Invalido'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Alumno->save($this->request->data)) {
				$this->Session->setFlash(__('El alumno ha sido guardado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El alumno no puede ser guardado. Intente otra vez.'));
			}
		} else {
			$this->request->data = $this->Alumno->read(null, $id);
		}
                
                 $apoderados=$this->Alumno->Apoderado->find('list',array('fields'=>'Apoderado.nombres'));
                
	$this->set(compact('apoderados'));
	}
	
        
        
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Alumno->id = $id;
		if (!$this->Alumno->exists()) {
			throw new NotFoundException(__('Alumno no válido'));
		}
		if ($this->Alumno->delete()) {
			$this->Session->setFlash(__('Alumno eliminado'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('El alumno no fue eliminado'));
		$this->redirect(array('action'  => 'index'));
	}
	}